package com.gonsin.i18n.test;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

public class DownloadSamples {

    @Test
    public void download(){

        String[] platforms = {"win_64",
                "win_32",
                "win_64_xp",
                "win_32_xp",
                "linux_64",
                "linux_32",
                "mac_64",
                "mac_arm64"
        };


        String[] versions = {
                "4280",
                "5304",
                "5060",
                "5414",
        };

        String rootUrl = "http://d2ettrnqo7v976.cloudfront.net/cef/";
        String filename = "cef_binary.7z";

        for(String p : platforms){
            for(String v : versions){
                String url = rootUrl + v + "/" + p + "/" + filename;
                String path = "cef" + File.separator + v + File.separator + p + File.separator + filename;

                try {
                    downloadFile(url, path);
                } catch (Exception ex) {

                }
            }
        }


    }

    private String downloadFile(String url, String path) throws IOException, URISyntaxException {



        try(CloseableHttpClient client = HttpClientBuilder.create().setProxy(new HttpHost("192.168.1.6", 8118)).build()) {
            HttpGet httpGet = new HttpGet(new URI(url));
            return client.execute(httpGet, new ResponseHandler<String>() {
                @Override
                public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
                    if(response.getStatusLine().getStatusCode() != 200){
                        return null;
                    }
                    File pathFile = new File(path);
                    File parentPath = pathFile.getParentFile();
                    if(!parentPath.exists()){
                        parentPath.mkdirs();
                    }
                    if(pathFile.exists()){
                        pathFile.delete();
                    }
                    if(!pathFile.exists()){
                        pathFile.createNewFile();
                    }
                    FileOutputStream out = new FileOutputStream(pathFile);

                    byte[] buffer = new byte[1024];
                    try(InputStream stream = response.getEntity().getContent()) {
                        int read = stream.read(buffer);
                        while(read != -1){
                            out.write(buffer, 0, read);
                            read = stream.read(buffer);
                        }
                        out.flush();
                        out.close();
                    }
                    return "OK";
                }
            });
        }
    }

}
